<?php defined('SYSPATH') OR die('No direct access allowed.');

set_time_limit(360);

function money($key){
	return number_format($key, 0, '.', ' ').'р.';
}

function formula($key){ 
/* 	$first_price = $key->it_first_price;
	$it_shop_price = $key->it_shop_price;
	return $it_shop_price - (($it_shop_price - $first_price)/100*25); */
	return $key->it_internet_price;
}

function getKeyWords($words){  
	return ','.str_replace(' ', ',', $words);
}

function do_latin($str) {
 
$latin_equivalent = array(
	32 => '_',
	95 => '_',
	168 => 'YO',
	184 => 'yo',
	192 => 'A',
	193 => 'B',
	194 => 'V',
	195 => 'G',
	196 => 'D',
	197 => 'E',
	198 => 'ZH',
	199 => 'Z',
	200 => 'I',
	201 => 'Y',
	202 => 'K',
	203 => 'L',
	204 => 'M',
	205 => 'N',
	206 => 'O',
	207 => 'P',
	208 => 'R',
	209 => 'S',
	210 => 'T',
	211 => 'U',
	212 => 'F',
	213 => 'KH',
	214 => 'TS',
	215 => 'CH',
	216 => 'SH',
	217 => 'SHCH',
	218 => '',
	219 => 'Y',
	220 => '',
	221 => 'E',
	222 => 'U',
	223 => 'YA',
	224 => 'a',
	225 => 'b',
	226 => 'v',
	227 => 'g',
	228 => 'd',
	229 => 'e',
	230 => 'zh',
	231 => 'z',
	232 => 'i',
	233 => 'y',
	234 => 'k',
	235 => 'l',
	236 => 'm',
	237 => 'n',
	238 => 'o',
	239 => 'p',
	240 => 'r',
	241 => 's',
	242 => 't',
	243 => 'u',
	244 => 'f',
	245 => 'kh',
	246 => 'ts',
	247 => 'ch',
	248 => 'sh',
	249 => 'shch',
	250 => '',
	251 => 'y',
	252 => '',
	253 => 'e',
	254 => 'u',
	255 => 'ya',
	185 => 'N'
);
 
	if ( ( $enc_str = iconv( 'UTF-8', 'CP1251', $str ) ) !== false ) {
		$str = $enc_str;
		unset( $enc_str );
	}
 
	$out = "";
	for( $i = 0; $i < strlen( $str ); $i++ ) {
		if( array_key_exists( ord( $str[ $i ]), $latin_equivalent ) ) {
			$out .= $latin_equivalent[ ord( $str[ $i ] ) ];
		} else {
			$out .= $str[ $i ];
		}
	}
    $out = htmlspecialchars_decode(trim($out));
    $trash = array('"', '#', '%',  '&',  "'",  '*',  ',', ':', ';',  '<',   '>', '?', '[',   '^',   '`',   '{',  '|',   '}');
    $out = str_replace($trash, "", $out);
	return($out);
}

define('TITLE_SHOP', 'C-NET');
define('DATE_FORMAT', 'd.m.Y');
define('DATE_FORMAT_DB', '%d.%m.%Y');
define('FILE_PRISE', 'C:/manual.chm');
define('MAIL_SUPPORT', 'support@c-net-shopping.ru');
define('MAIL_ADMIN', 'ivan.chura@webnizer.com');
define('MAIL_MARKET', 'koshkiny@mail.ru');

define('SERVER_PATH', '/home/webnizer/public_html/c-net/');
define('SERVER_SITE', 'baza.ru');

define('PATH_IMG', '/home/webnizer/c-net/Koshkin/img/');
define('PATH_HTML', '/home/webnizer/c-net/Koshkin/html/');
define('PATH_HTML_IMG', '/home/webnizer/c-net/Koshkin/html_img/'); 
define('PATH_ORDER', '/home/webnizer/c-net/Koshkin/orders/');

define('PATH_BAZA', '/home/webnizer/c-net/baza/');

header('X-Powered-By: Turbo Pascal 7.01'); 

$etag=md5($_SERVER['REQUEST_URI']);
$etag=substr($etag, 0, 8) . '-' . substr($etag, 8, 7) . '-' . substr($etag, 15, 8);
header('ETag: "' . $etag . '"');
 
header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()-10) . " GMT");
#header("Expires: " . gmdate("D, d M Y H:i:s", time() + 5) . " GMT");
header("Date: " . gmdate("D, d M Y H:i:s", time() + 5) . " GMT");
#header("Pragma: no-cache");
#header("Cache-Control: max-age=1, s-maxage=1, cache, must-revalidate");
 
abstract class Web_Controller extends Template_Controller {
	
	var $groups = null;
	
	public function __construct()
	{	 
		
		parent::__construct();
		 
		Kohana::config_set('locale', 'ru_RU');
		
		$this->input = new Input();
 
		$this->session = new Session;
		
		if(count($this->session->get('items')) == 0){
			$this->session->set('items', array());
		}
		 
		if(($this->session->get('locale')) == null){
			$this->session->set('locale', 'ru_RU');
		}   
 
		$this->db = new Database; 
/*
               $result = $this->db->select('*')->from('bz_firms')->get();
                  $result = $this->db->select('*')->from('bz_items')->get();
              $result = $this->db->select('*')->from('bz_news')->get();
               $result = $this->db->select('*')->from('bz_types')->get();

                foreach ($result as $row) {

                    $this->db->query('update bz_firms set fr_link = \''.mysql_escape_string(do_latin($row->fr_title)).'\' where fr_id = '.$row->fr_id);
                   $this->db->query('update bz_items set it_link = \''.mysql_escape_string(do_latin($row->it_model)).'\' where it_id = '.$row->it_id);
                   $this->db->query('update bz_news set nw_link_local = \''.mysql_escape_string(do_latin($row->nw_title)).'\' where nw_id = '.$row->nw_id);
                    $this->db->query('update bz_types set tp_link = \''.mysql_escape_string(do_latin($row->tp_title)).'\' where tp_id = '.$row->tp_id);

               }*/



		$table = 'bz_groups';
 
		$this->groups = $this->db->select('*')->from($table)->orderby('gr_sorting')->orderby('gr_title')->get(); 
		
		$this->template->groups = $this->groups;
		$this->selected_page = null;
		
		$val = $this->session->get('items');
		if(empty($val)){
			$this->count_bin_items = 0;
		}else{
			$this->count_bin_items = count($val);
		}
		try {
			 
 			if (!empty($val)) {
			    $archive_items = $this->db->select('*')->from('bz_items')->where('it_archive = 1')->in('it_id', array_keys($val))->get()->count();
			    if ($archive_items) {
				    $this->session->set('items', array());
				    url::redirect('?catalog_updated');
			    };
			}  
		} catch(Exception $e) {
			 
		}
 
	}
	
	public function _render(){
		header("Title: ".do_latin($this->template->title));
		parent::_render();
	}
}

?>